package com.smartlight.godlight.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.smartlight.godlight.R;
import com.smartlight.godlight.listener.OnItemClickListener;
import com.smartlight.godlight.model.Light;

import java.util.List;

public class GridImageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<Light> buddhas;
    private Context context;
    private static OnItemClickListener onItemClickListener;
    private int columnCount;

    private static final int TYPE_ITEM = 0;
    private static final int TYPE_FOOTER = 1;
    private boolean isFooterViewVisible = false; // 控制Footer View的显示状态

    public GridImageAdapter(Context context, List<Light> buddhas, int columnCount) {
        this.context = context;
        this.buddhas = buddhas;
        this.columnCount = columnCount;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_image_item, parent, false);
//        return new ImageViewHolder(view);

        if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_image_item, parent, false);
            return new ImageViewHolder(view);
        } else if (viewType == TYPE_FOOTER) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.load_more, parent, false); // Footer布局文件
            return new FooterViewHolder(view);
        }
//        return null;
        return null;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == getItemCount() - 1 && isFooterViewVisible) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        // 设置item
        if (holder instanceof ImageViewHolder) {
            Glide.with(context) // 使用Glide加载图片库加载图片
                    .load(buddhas.get(position).getImg())
                    .into(((ImageViewHolder) holder).imageView);
            ((ImageViewHolder) holder).buddhaNo.setText(buddhas.get(position).getName());
        }
        // 设置loadmore
        else if (holder instanceof FooterViewHolder) {
            ((FooterViewHolder) holder).bind(isFooterViewVisible); // 更新footer view的状态或内容
        }
    }

    @Override
    public int getItemCount() {
        return buddhas.size();
    }

    public void setFooterViewVisible(boolean visible) {
        isFooterViewVisible = visible;
        notifyItemChanged(getItemCount() - 1); // 通知适配器footer view的状态改变，重新绑定数据到footer view holder上（如果可见）
    }

    public void setOnItemClickListener(OnItemClickListener listenser) {
        onItemClickListener = listenser;
    }

    static class ImageViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView buddhaNo;
        ImageViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.imageView); // 确保你的布局文件中有imageView这个ID的ImageView控件
            buddhaNo = itemView.findViewById(R.id.buddhaNo);

            // 监听item点击事件
            // item点击监听
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(getAdapterPosition());
                    }
                }
            });
        }
    }

    static class FooterViewHolder extends RecyclerView.ViewHolder {
        TextView tvFooter; // 引用footer中的TextView等...

        FooterViewHolder(View itemView) {
            super(itemView);
            tvFooter = itemView.findViewById(R.id.tvFooter); // 初始化视图组件等...
        }

        void bind(boolean isVisible) { // 根据isVisible更新footer view的显示状态或内容等... }
        }
    }
}